home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / GRAPHICS.SWG / 0069_3D Graphics Box.pas < prev    next >
Pascal/Delphi Source File  |  1994-01-27  |  7KB  |  203 lines

  1. {
  2. From: PETER KOLDING
  3. Subj: 3D Graphics
  4.  
  5. MB>  Hello, I'm trying to write a simple program that will plot points in three
  6. MB>  dimensions and allow you to rotate them,or view them from different
  7. angles.
  8. MB>  need a lot of help. I'm trying to make a data file of points in the format
  9. MB>  (x,y,z) and then have the program read the points in to display. So far no
  10. MB>  luck. If anyone has any code that is simple enough for me to understand I
  11. MB>  would appreciate it. Also if anyone has any code for doing fast vga
  12. MB>  animations(in assembly) could they please post it? Thanks in advance.
  13. }
  14.  
  15. program boxrot;
  16.  
  17. {PUBLIC DOMAIN  1993 Peter M. Gruhn}
  18.  
  19. {Program draws a box on screen. Allows user to rotate the box around
  20.  the three primary axes. Viewing transform is simple ignore z.}
  21.  
  22. {I used _Computer_Graphics:_Principles_and_Practice_, Foley et al
  23.  ISBN 0-201-12110-7 as a reference}
  24.  
  25. {RUNNING:
  26.  Borland Pascal 7. Should run on any graphics device supported by BGI.
  27.  If you have smaller than 280 resolution, change '+200' to something
  28.  smaller and/or change 75 to something smaller.
  29.  
  30.  Since this machine is
  31.  not really set up for doing DOS graphics, I hard coded my BGI path, so
  32.  you have to find 'initgraph' and change the bgi path to something that
  33.  works on your machine. Try ''.
  34.  
  35.  
  36. {Okey dokey. This is kinda slow, and does a nice job of demonstrating the
  37.  problems of repeatedly modifying the same data set. That is, the more and
  38.  more you rotate the box, the more and more distorted it gets. This is
  39.  because computers are not perfect at calculations, and all of those little
  40.  errors add up quite quickly.
  41.  
  42.  It's because of that that I used reals, not reals. I used floating point
  43.  because the guy doesn't know what is going on at all with 3d, so better to
  44.  look at only the math that is really happening. Besides, I still have to
  45.  think to use fixed point. Whaddaya want for .5 hour programming.
  46.  
  47.  DIRECTIONS:
  48.    ',' - rotates around the x axis
  49.    '.' - rotates around the y axis
  50.    '/' - rotates around the z axis
  51.    'q' - quits
  52.  
  53.    All rotations are done around global axes, not object axes.}
  54.  
  55. uses graph,crt;
  56.  
  57. const radtheta=1{degrees}*3.1415926535{radians}/180{per degrees};
  58.       {sin and cos on computers are done in radians.}
  59.  
  60. type tpointr=record   {Just a record to hold 3d points}
  61.        x,y,z:real;
  62.        end;
  63.  
  64. var box:array[0..7] of tpointr;   {The box we will manipulate}
  65.     c:char;                    {Our input mechanism}
  66.  
  67. procedure init;
  68. var gd,gm:integer;
  69. {turns on graphics and creates a cube. Since the rotation routines
  70.  rotate around the origin, I have centered the cube on the origin, so
  71.  that it stays in place and only spins.} begin
  72.   gd:=detect; initgraph(gd,gm,'d:\turbo\tp\');
  73.   box[0].x:=-75;  box[0].y:=-75;  box[0].z:=-75;
  74.   box[1].x:=75;   box[1].y:=-75;  box[1].z:=-75;
  75.   box[2].x:=75;   box[2].y:=75;   box[2].z:=-75;
  76.   box[3].x:=-75;  box[3].y:=75;   box[3].z:=-75;
  77.   box[4].x:=-75;  box[4].y:=-75;  box[4].z:=75;
  78.   box[5].x:=75;   box[5].y:=-75;  box[5].z:=75;
  79.   box[6].x:=75;   box[6].y:=75;   box[6].z:=75;
  80.   box[7].x:=-75;  box[7].y:=75;   box[7].z:=75; end;
  81.  
  82. procedure myline(x1,y1,z1,x2,y2,z2:real); {Keeps the draw routine pretty.
  83. Pixels are integers, so I round. Since the
  84.  cube is centered around 0,0 I move it over 200 to put it on screen.} begin
  85. {if you think those real mults are slow, here's some rounds too...}
  86.  
  87. {hey, you may wonder, what happened to the stinking z coordinate? Ah, says I,
  88.  this is the simplest of 3d viewing transforms. You just take the z coord out
  89.  of things and boom. Looking straight down the z axis on the object. If I get
  90.  inspired, I will add simple perspective transform to these.} {There, got
  91. inspired. Made mistakes. Foley et al are not very good at
  92.  tutoring perspective and I'm kinda ready to be done and post this.}
  93.   line(round(x1)+200,round(y1)+200,
  94.        round(x2)+200,round(y2)+200);
  95. end;
  96.  
  97. procedure draw;
  98. {my model is hard coded. No cool things like vertex and edge and face
  99.  lists.}
  100.  
  101. begin
  102.   myline(box[0].x,box[0].y,box[0].z, box[1].x,box[1].y,box[1].z);
  103.   myline(box[1].x,box[1].y,box[1].z, box[2].x,box[2].y,box[2].z);
  104.   myline(box[2].x,box[2].y,box[2].z, box[3].x,box[3].y,box[3].z);
  105.   myline(box[3].x,box[3].y,box[3].z, box[0].x,box[0].y,box[0].z);
  106.  
  107.   myline(box[4].x,box[4].y,box[4].z, box[5].x,box[5].y,box[5].z);
  108.   myline(box[5].x,box[5].y,box[5].z, box[6].x,box[6].y,box[6].z);
  109.   myline(box[6].x,box[6].y,box[6].z, box[7].x,box[7].y,box[7].z);
  110.   myline(box[7].x,box[7].y,box[7].z, box[4].x,box[4].y,box[4].z);
  111.  
  112.   myline(box[0].x,box[0].y,box[0].z, box[4].x,box[4].y,box[4].z);
  113.   myline(box[1].x,box[1].y,box[1].z, box[5].x,box[5].y,box[5].z);
  114.   myline(box[2].x,box[2].y,box[2].z, box[6].x,box[6].y,box[6].z);
  115.   myline(box[3].x,box[3].y,box[3].z, box[7].x,box[7].y,box[7].z);
  116.  
  117.   myline(box[0].x,box[0].y,box[0].z, box[5].x,box[5].y,box[5].z);
  118.   myline(box[1].x,box[1].y,box[1].z, box[4].x,box[4].y,box[4].z); end;
  119.  
  120. procedure rotx;
  121. {if you know your matrix multiplication, the following equations
  122.  are derived from
  123.  
  124.  [x   [ 1  0  0  0   [x',y',z',1]
  125.   y     0  c -s  0 =
  126.   z     0  s  c  0
  127.   1]    0  0  0  1]
  128. }
  129. var i:integer;
  130. begin
  131.   setcolor(0);
  132.   draw;
  133.   for i:=0 to 7 do
  134.     begin
  135.     box[i].x:= box[i].x;
  136.     box[i].y:= box[i].y*cos(radTheta) + box[i].z*sin(radTheta);
  137.     box[i].z:=-box[i].y*sin(radTheta) + box[i].z*cos(radTheta);
  138.     end;
  139.   setcolor(15);
  140.   draw;
  141. end;
  142.  
  143. procedure roty;
  144. {if you know your matrix multiplication, the following equations
  145.  are derived from
  146.  [x   [ c  0  s  0   [x',y',z',1]
  147.   y     0  1  0  0 =
  148.   z    -s  0  c  0
  149.   1]    0  0  0  1]
  150. }
  151. var i:integer;
  152. begin
  153.   setcolor(0);
  154.   draw;
  155.   for i:=0 to 7 do
  156.     begin
  157.     box[i].x:= box[i].x*cos(radTheta) - box[i].z*sin(radTheta);
  158.     box[i].y:= box[i].y;
  159.     box[i].z:= box[i].x*sin(radTheta) + box[i].z*cos(radTheta);
  160.     end;
  161.   setcolor(15);
  162.   draw;
  163. end;
  164.  
  165. procedure rotz;
  166. {if you know your matrix multiplication, the following equations
  167.  are derived from
  168.  
  169.  [x   [ c -s  0  0   [x',y',z',1]
  170.   y     s  c  0  0 =
  171.   z     0  0  1  0
  172.   1]    0  0  0  1]
  173. }
  174. var i:integer;
  175. begin
  176.   setcolor(0);
  177.   draw;
  178.   for i:=0 to 7 do
  179.     begin
  180.     box[i].x:= box[i].x*cos(radTheta) + box[i].y*sin(radTheta);
  181.     box[i].y:=-box[i].x*sin(radTheta) + box[i].y*cos(radTheta);
  182.     box[i].z:= box[i].z;
  183.     end;
  184.   setcolor(15);
  185.   draw;
  186. end;
  187.  
  188.  
  189. begin
  190.   init;
  191.   setcolor(14); draw;
  192.   repeat
  193.     c:=readkey;
  194.     case c of
  195.       ',' : rotx;
  196.       '.' : roty;
  197.       '/' : rotz;
  198.       else {who gives a};
  199.       end; {case}
  200.   until c='q';
  201.   closegraph;
  202. end.
  203.